package product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

public class ProductUpdate extends JInternalFrame {

	ArrayList<ProductData> pro;
	DefaultTableModel model;// JTable에 입력을위해
	Vector<String> col = new Vector<String>();
	DecimalFormat df = new DecimalFormat("###,###,###.##");

	private JTextField findStr;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Create the frame.
	 */
	public ProductUpdate(ArrayList<ProductData> p) {
		super("ProductUpdate", true, true, true, false);
		this.pro = p;
		col.add("제품코드");
		col.add("제품명");
		col.add("단가");
		col.add("수량");
		col.add("금액");
		col.add("생산일자");
		col.add("생산번호");
		model = new DefaultTableModel(col, 0) {
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		setBounds(100, 100, 488, 312);
		getContentPane().setLayout(null);
		getContentPane().add(getFindStr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());

		// 테이블 형태지정
		DefaultTableCellRenderer Center = new DefaultTableCellRenderer();
		Center.setHorizontalAlignment(SwingConstants.CENTER);// 가운데 정렬
		DefaultTableCellRenderer Right = new DefaultTableCellRenderer();
		Right.setHorizontalAlignment(SwingConstants.RIGHT);// 우측 정렬
		TableColumnModel tcmSchedule = table.getColumnModel();
		tcmSchedule.getColumn(0).setCellRenderer(Center);
		tcmSchedule.getColumn(1).setCellRenderer(Center);
		tcmSchedule.getColumn(2).setCellRenderer(Right);// 단가
		tcmSchedule.getColumn(3).setCellRenderer(Center);
		tcmSchedule.getColumn(4).setCellRenderer(Right);// 금액
		tcmSchedule.getColumn(5).setCellRenderer(Center);
		tcmSchedule.getColumn(6).setCellRenderer(Center);

		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
		table.getColumn("제품코드").setPreferredWidth(50);
		table.getColumn("제품명").setPreferredWidth(50);
		table.getColumn("단가").setPreferredWidth(70);
		table.getColumn("수량").setPreferredWidth(30);
		table.getColumn("금액").setPreferredWidth(70);
		table.getColumn("생산일자").setPreferredWidth(50);
		table.getColumn("생산번호").setPreferredWidth(30);
		table.setRowHeight(20);// 테이블 상하폭지정
	}

	public String replace(String s) {
		String temp = s.replace(",", "");
		String temp2 = temp.replace("원", "");
		return temp2;
	}

	public void search() {

		if (model.getRowCount() > 0) { // 행의 수
			for (int i = model.getRowCount() - 1; i > -1; i--) {
				model.removeRow(i);// 테이블을 초기화
			}
		}
		String find = findStr.getText();
		for (int i = 0; i < pro.size(); i++) {// ArrayList에 저장되어있는 정보들을 JTable에
												// 추가
			Vector<String> row = new Vector<String>();
			ProductData value = pro.get(i);// index번호 순서대로

			if (value.getProCode().contains(find)
					|| value.getProName().contains(find)
					|| value.getProDate().contains(find)) {
				row.add(value.getProCode());
				row.add(value.getProName());
				String pstr = df.format((double) value.getProPrice());
				row.add(pstr + "원");
				row.add(String.valueOf(value.getProEa()));
				String tstr = df.format((double) value.getProTot());
				row.add(tstr + "원");
				row.add(value.getProDate());
				row.add(String.valueOf(value.getCount()));
				model.addRow(row);
			}
		}
	}

	public void mouseC() {
		int index = table.getSelectedRow();// 선택된 열의 table index번호를 저장
		Vector<String> vec = new Vector<String>();// 벡터생성
		vec.add((String) table.getValueAt(index, 0));
		vec.add((String) table.getValueAt(index, 1));
		vec.add(replace((String) table.getValueAt(index, 2)));
		vec.add((String) table.getValueAt(index, 3));
		vec.add(replace((String) table.getValueAt(index, 4)));
		vec.add((String) table.getValueAt(index, 5));
		vec.add((String) table.getValueAt(index, 6));
		// 자리별 정보를 Vector에 추가

		ProductTableAction pta = new ProductTableAction(pro, vec, model);// ArrayList와
																			// Vector를
																			// 매개변수로
																			// 전달
		pta.setVisible(true);

	}

	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(12, 10, 204, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					search();// 조회 매서드
				}
			});
			btnNewButton.setBounds(228, 9, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 41, 448, 220);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					mouseC();// 마우스 클릭 매서드
				}
			});
		}
		return table;
	}
}
